package com.halen.user.controller;


import com.halen.user.entity.Student;
import com.halen.user.entity.User;
import com.halen.user.service.StudentService;
import com.halen.user.service.TeacherService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("user")
public class UserController {





    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;
    @RequestMapping("/test")
    public String testUser(@RequestBody Student student) {

        studentService.createStudent(student);

        return "user";
    }




    @RequestMapping("/demo/{id}/{name}")
    public String demoUser(ModelMap modelMap, @PathVariable Integer id, @PathVariable String name) {
        System.out.println(id);
        System.out.println(name);
        User user = new User();
        user.setId(32);
        user.setName("拉力计恩考");
        modelMap.addAttribute("user",user);
        return "demo";
    }

    @RequestMapping("/demo2")
    public String demo2User() {
       /* String id = request.getParameter("id");
        String name = request.getParameter("name");*/
        System.out.println("fdfkj");
       /* User user = new User();
        user.setId(28);
        user.setName("laowang");
        modelMap.addAttribute("user", user);*/

        return "demo";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        String name = file.getOriginalFilename();
        System.out.println(name);
        File uploadF = new File("/" + name);
        file.transferTo(uploadF);
        return "demo";
    }

    @RequestMapping("down")
    public ResponseEntity<byte[]> download() throws IOException {
        String path = "K:/1.png";
        File file = new File(path);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment",path);
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }
}
